package com.rbs.controller;

import com.rbs.entity.Book;
import com.rbs.entity.Cart;
import com.rbs.entity.CartItem;
import com.rbs.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    public BookService bookService;

    @RequestMapping("/addCart")
    public String addCart(Integer bookId, HttpSession session) {
        //获取session中购物车对象
        Cart cart = (Cart) session.getAttribute("cart");
        //判断购物车是否为null
        if (cart == null) {
            //如果购物车为null 创建购物车对象
            cart = new Cart();
            session.setAttribute("cart", cart);
        }
        //把购买的图书放入购物车
        //判断购物车是否已经有该图书
        //查看buyMap是否有该图书的id
        Map<Integer, CartItem> buyMap = cart.getBuyMap();
        if (buyMap.containsKey(bookId)) {
            CartItem cartItem = buyMap.get(bookId);
            cartItem.setCount(cartItem.getCount() + 1);
        } else {
            //书不存在
            CartItem cartItem = new CartItem();
            Book book = bookService.selectBookById(bookId);
            cartItem.setBook(book);
            cartItem.setCount(1);
            //将cartItem放入buyMap中
            buyMap.put(bookId, cartItem);
        }
        //计算总价
        Double totalMoney = 0.0;
        //计算节省的价钱
        Double saveMoney = 0.0;
        Set<Map.Entry<Integer, CartItem>> entries = buyMap.entrySet();
        for (Map.Entry<Integer, CartItem> entry : entries) {
            //图书现价
            Double dangPrice = entry.getValue().getBook().getDangPrice();
            //图书原价
            Double bookPrice = entry.getValue().getBook().getBookPrice();
            //书的数量
            Integer count = entry.getValue().getCount();
            //总价
            totalMoney += dangPrice * count;
            //节省的价钱
            saveMoney += (bookPrice - dangPrice) * count;
        }
        cart.setTotalMoney(totalMoney);
        cart.setSaveMoney(saveMoney);
        return "redirect:/cart/cart_list.jsp";
    }

    @RequestMapping("/removeMap")
    public String removeMap(int bookId, HttpSession session) {
        //获取购物车
        Cart cart = (Cart) session.getAttribute("cart");
        //从购物车集合中获取要删除的商品项
        Map<Integer, CartItem> buyMap = cart.getBuyMap();
        CartItem cartItem = buyMap.get(bookId);
        buyMap.remove(bookId);
        //把要删除的商品放到删除集合中
        Map<Integer, CartItem> removeMap = cart.getRemoveMap();
        removeMap.put(bookId, cartItem);
        //计算总价
        Double totalMoney = 0.0;
        //计算节省价钱
        Double saveMoney = 0.0;
        Set<Map.Entry<Integer, CartItem>> entries = buyMap.entrySet();
        for (Map.Entry<Integer, CartItem> entry : entries) {
            Double dangPrice = entry.getValue().getBook().getDangPrice();
            Double bookPrice = entry.getValue().getBook().getBookPrice();
            Integer count = entry.getValue().getCount();
            //总价
            totalMoney += dangPrice * count;
            //节省价钱
            saveMoney += (bookPrice - dangPrice) * count;
        }
        cart.setTotalMoney(totalMoney);
        cart.setSaveMoney(saveMoney);
        return "redirect:/cart/cart_list.jsp";
    }

    @RequestMapping("/updateCart")
    public String updateCart(int bookId, int count, HttpSession session) {
        Cart cart = (Cart) session.getAttribute("cart");
        //获取要变更的商品项对象
        Map<Integer, CartItem> buyMap = cart.getBuyMap();
        CartItem cartItem = buyMap.get(bookId);
        cartItem.setCount(count);
        //计算总价
        Double totalMoney = 0.0;
        //计算节省价钱
        Double saveMoney = 0.0;
        Set<Map.Entry<Integer, CartItem>> entries = buyMap.entrySet();
        for (Map.Entry<Integer, CartItem> entry : entries) {
            Double dangPrice = entry.getValue().getBook().getDangPrice();
            Double bookPrice = entry.getValue().getBook().getBookPrice();
            Integer count1 = entry.getValue().getCount();
            //总价
            totalMoney += dangPrice * count1;
            //节省价钱
            saveMoney += (bookPrice - dangPrice) * count1;
        }
        cart.setTotalMoney(totalMoney);
        cart.setSaveMoney(saveMoney);
        return "redirect:/cart/cart_list.jsp";
    }
    @RequestMapping("recoverCart")
    public String recoverCart(int bookId,HttpSession session){
        Cart cart = (Cart) session.getAttribute("cart");
        Map<Integer, CartItem> removeMap = cart.getRemoveMap();
        CartItem cartItem = removeMap.get(bookId);
        Map<Integer, CartItem> buyMap = cart.getBuyMap();
        buyMap.put(bookId,cartItem);
        removeMap.remove(bookId);
        //计算总价
        Double totalMoney = 0.0;
        //计算节省价钱
        Double saveMoney = 0.0;
        Set<Map.Entry<Integer, CartItem>> entries = buyMap.entrySet();
        for (Map.Entry<Integer, CartItem> entry : entries) {
            Double dangPrice = entry.getValue().getBook().getDangPrice();
            Double bookPrice = entry.getValue().getBook().getBookPrice();
            Integer count1 = entry.getValue().getCount();
            //总价
            totalMoney += dangPrice * count1;
            //节省价钱
            saveMoney += (bookPrice - dangPrice) * count1;
        }
        cart.setTotalMoney(totalMoney);
        cart.setSaveMoney(saveMoney);
        return "redirect:/cart/cart_list.jsp";
    }
}